/*
* 地图按钮事件定义
*
*
*
*
*/

//实现地图上的控件，切换 浏览 1、测距 2、标注 3
function btnInit(toolSign) {
	if (toolSign === 1) {
		//浏览
	    //Gid("btnMouse").src = "../../images/control2/pan_2.png";
	    //Gid("btnSpot").src = "../../images/control2/spot_1.png";
	    //Gid("btnRanging").src = "../../images/control2/distance_1.png";
	    Gid("btnMouse").src = iconUrlHead + "images/control2/pan_2.png";
	    Gid("btnSpot").src = iconUrlHead + "images/control2/spot_1.png";
	    Gid("btnRanging").src = iconUrlHead + "images/control2/distance_1.png";

		removeMapClickEvent(); //移除地图单击事件
		map.setDefaultCursor(panCursor);
		//恢复默认样式

	} else if (toolSign === 2) {
		//测距
	    //Gid("btnMouse").src = "../../images/control2/pan_1.png";
	    //Gid("btnSpot").src = "../../images/control2/spot_1.png";
	    //Gid("btnRanging").src = "../../images/control2/distance_2.png";
	    Gid("btnMouse").src = iconUrlHead + "images/control2/pan_1.png";
	    Gid("btnSpot").src = iconUrlHead + "images/control2/spot_1.png";
	    Gid("btnRanging").src = iconUrlHead + "images/control2/distance_2.png";

		removeMapClickEvent();
		map.setDefaultCursor(panCursor);
		//恢复默认样式
	} else if (toolSign === 3) {
		//标注
		//Gid("btnMouse").src = "../../images/control2/pan_1.png";
		//Gid("btnSpot").src = "../../images/control2/spot_2.png";
		//Gid("btnRanging").src = "../../images/control2/distance_1.png";
	    Gid("btnMouse").src = iconUrlHead + "images/control2/pan_1.png";
	    Gid("btnSpot").src = iconUrlHead + "images/control2/spot_2.png";
	    Gid("btnRanging").src = iconUrlHead + "images/control2/distance_1.png";
		if (myDis)
			myDis.close();
	}
}

//开始鼠标移图方式
function btnMoveMap() {
	btnInit(1);
}

//地图放大一个级别 按钮点下
function btnPlusDown() {
    //Gid("btnPlus").src = "../../images/control2/zoomin_2.png";
    Gid("btnPlus").src = iconUrlHead + "images/control2/zoomin_2.png";
	map.zoomIn();
}

//地图放大 按钮松开
function btnPlusUp() {
    //Gid("btnPlus").src = "../../images/control2/zoomin_1.png";
    Gid("btnPlus").src = iconUrlHead + "images/control2/zoomin_1.png";
}

//地图缩小一个级别  按钮点下
function btnSubtractDown() {
    //Gid("btnSubtract").src = "../../images/control2/zoomout_2.png";
    Gid("btnSubtract").src = iconUrlHead + "images/control2/zoomout_2.png";
	map.zoomOut();
}

//地图缩小  按钮松开
function btnSubtractUp() {
    //Gid("btnSubtract").src = "../../images/control2/zoomout_1.png";
    Gid("btnSubtract").src = iconUrlHead + "images/control2/zoomout_1.png";
}

// //取坐标，按下
// function btnGetCoorDown () {
// Gid("btnGetCoor").src = "images/control/getcoor_2.png";
// var defaultCursor = map.getDefaultCursor();//保存默认鼠标样式
// map.setDefaultCursor("crosshair");//设置鼠标样式为十字形
// btnInit(false);
// //添加地图单击事件
// map.addEventListener("click",
// clickevent=function (e) {
// var pt = e.point;
// //解析成gps坐标 先用百度转百度 减去偏移量
// BMap.Convertor.translate(pt,0, function (point){
// var gpsx = pt.lng * 2 - point.lng ;
// var gpsy = pt.lat * 2 - point.lat ;
// alert("百度坐标X:" + pt.lng + ",百度坐标Y:" + pt.lat + "\r\nGPS坐标X:" + gpsx + ",GPS坐标Y:" + gpsy);
// });
// map.removeEventListener("click", clickevent);//移除单击事件
// map.setDefaultCursor(defaultCursor);//恢复默认样式
// btnInit(true);
// }
// );
// }
//
// function btnGetCoorUp() {
// Gid("btnGetCoor").src = "images/control/getcoor_1.png";
// }

//搜索，按下
function btnSearchDown() {
    //Gid("btnSearch").src = "../../images/control2/search2_2.png";
    Gid("btnSearch").src = iconUrlHead + "images/control2/search2_2.png";

	$('#addressquery2').window("open");
	/*
	 if (Gid("addressquery2").style.display == ""){
	 btnCloseSearchDiv2();
	 }
	 else {
	 btnShowSearchDiv2();
	 }
	 */
}

function btnSearchUp() {
    //Gid("btnSearch").src = "../../images/control2/search2_1.png";
    Gid("btnSearch").src = iconUrlHead + "images/control2/search2_1.png";
}

//打开搜索输入框
function btnShowSearchDiv(searchvalue) {
	Gid("addressquery").style.display = "";
	if (searchvalue) {
		Gid('querycontent').value = searchvalue;
		localSearch();
	} else {
		Gid('querycontent').value = "";
	}
}

//打开搜索输入框
function btnShowSearchDiv2(searchvalue) {
	//Gid("addressquery2").style.display = "";
	$('#addressquery2').window("open");
	if (searchvalue) {
		//Gid('querycontent2').value = searchvalue;
		$("#querycontent2").searchbox("setValue", searchvalue);
		localSearchOnlyData();
	} else {
		//Gid('querycontent2').value = "";
		$("#querycontent2").searchbox("clear");
	}
}

//关闭搜索输入框
function btnCloseSearchDiv() {
	clearSearch();
	Gid('addressquery').style.display = "none";
	Gid('querycontent').value = "";
}

//关闭搜索输入框2
function btnCloseSearchDiv2() {
	clearSearch2();
	//Gid('addressquery2').style.display = "none";
	//Gid('querycontent2').value = "";
	$("#addressquery2").hide();
	//$("#querycontent2").searchbox("clear");
}

//开始定位
function btnBeginSpot() {

	//map.removeEventListener("click", mapClickEvent);//移除单击事件
	removeMapClickEvent();
	map.setDefaultCursor("crosshair");
	//设置鼠标样式为十字形

	btnInit(3);
	//初始化按钮，不选择pan

	//添加地图单击事件
	//map.addEventListener("click", mapClickEvent);
	addMapClickEvent();
}

//开启测量距离
function btnBeginDistance() {
	myDis = new BMapLib.DistanceTool(map);
	myDis.open();
	//开启鼠标测距
	//myDis.close();  //关闭鼠标测距大
	btnInit(2);

	myDis.addEventListener("drawend", function(e) {
		btnInit(1);
	});
}

//2016-7-26 zch add  volunteer  start
function btnVolunteerDown() {
    //Gid("btnVolunteer").src = "../../images/control2/volun_2.png";
    Gid("btnVolunteer").src = iconUrlHead + "images/control2/volun_2.png";

	$('#volunteerWindow').window("open");
	$("#volunteerGrid").datagrid("loadData", volunteerArray.slice(0, 10));
	var pager = $("#volunteerGrid").datagrid("getPager");
	pager.pagination({
		total : volunteerArray.length,
		onSelectPage : function(pageNo, pageSize) {
			var start = (pageNo - 1) * pageSize;
			var end = start + pageSize;
			$("#volunteerGrid").datagrid("loadData", volunteerArray.slice(start, end));
			pager.pagination('refresh', {
				total : volunteerArray.length,
				pageNumber : pageNo
			});
		}
	});
	//加载所有待命的志愿者
	//map.centerAndZoom(new BMap.Point(initX, initY), initZoom);
	//将所有待命志愿者数组 加入到聚合内中
	// var markerClusterer = new BMapLib.MarkerClusterer(map, {
	// markers : volunteerArray
	//
	// });
	// map.addOverlay(markerClusterer);

}

function btnVolunteerUp() {
    //Gid("btnVolunteer").src = "../../images/control2/volun_1.png";
    Gid("btnVolunteer").src = iconUrlHead + "images/control2/volun_1.png";
}

// end

function btnAmbulanceDown() {
    //Gid("btnAmbulance").src = "../../images/control2/amb_2.png";
    Gid("btnAmbulance").src = iconUrlHead + "images/control2/amb_2.png";
    //$('#ambPanel').panel('open');

    //add2021-01-16 plq 车辆列表可见状态的切换
    if ($('#ambPanel').parent(".panel").css("display") == "block") {
        $('#ambPanel').panel('close');
    }
    else {
        $('#ambPanel').panel('open');
    }
	/*
	if (isAmbListOpen) {
	$('#ambPanel').panel('close');
	}
	else{

	$('#ambPanel').panel('open');
	}

	isAmbListOpen = !isAmbListOpen;
	*/
	//var path = '[{"lng":116.405865,"lat":39.911557},{"lng":116.405784,"lat":39.913317},{"lng":116.405753,"lat":39.913977},{"lng":116.405753,"lat":39.914037},{"lng":116.405753,"lat":39.914127},{"lng":116.405753,"lat":39.914127},{"lng":116.405503,"lat":39.914116},{"lng":116.403934,"lat":39.914074},{"lng":116.402216,"lat":39.914019}]';
	//showWaitAmbSuggestion(116.290863,40.04335, 116.38199, 39.9033 );
	//showWaitAmbSuggestionHis(path, '历史数据');

}

function btnAmbulanceUp() {
    //Gid("btnAmbulance").src = "../../images/control2/amb_1.png";
    Gid("btnAmbulance").src = iconUrlHead + "images/control2/amb_1.png";
}

function querycontentKeyDown() {
	if (event.keyCode == 13) {
		event.returnValue = false;
		event.cancel = true;
		localSearch();
	}
}

function querycontentKeyDown2() {
	if (event.keyCode == 13) {
		event.returnValue = false;
		event.cancel = true;
		localSearchOnlyData();
	}
}

//2016-7-26 zch  add  添加志愿到grid   start
// function addVolunToGrid(userid, name, volunteerType,  telphone) {
// $("#volunteerGrid").datagrid("loadData",volunteerArray);
// }
// var rr = {};
// rr.userid = userid;
// rr.name = name;
// rr.volunteerType = volunteerType;
// rr.telphone = telphone;
// if (volunteerArray.length > 0) {
// //以下为本地分页
// $("#volunteerGrid").datagrid("insertRow", {
// index : 0,
// row : rr
// });
// var pager = $("#volunteerGrid").datagrid("getPager");
// pager.pagination({
// total : volunteerArray.length,
// onSelectPage : function(pageNo, pageSize) {
// var start = (pageNo - 1) * pageSize;
// var end = start + pageSize;
// $("#volunteerGrid").datagrid("loadData", volunteerArray.slice(start, end));
// pager.pagination('refresh', {
// total : volunteerArray.length,
// pageNumber : pageNo
// });
// }
// });
// }
// }

function deleteVolunFromGrid(userid) {
	//由于存在分页数据， 无法获取异页的数据进行删除， 将整个清除，重新加载一遍全局数组 volunteerArray[]
	$('#volunteerGrid').datagrid("loadData", {
		total : 0,
		rows : []
	});
	$('#volunteerGrid').datagrid('loadData', volunteerArray);
}

//根据 出生年月计算年龄
// function exchangeAges(str) {
// var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
// if (r == null)
// return false;
// var d = new Date(r[1], r[3] - 1, r[4]);
// if (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]) {
// var Y = new Date().getFullYear();
// return ((Y - r[1]));
// }
// return ("输入的日期格式错误！");
// }

//	end

function addAmbToTree(ambId, ambRealSign, stationId, stationName) {
	var roots = $("#ambTree").tree("getRoots");
	var findStation = false;
	if (roots && roots.length > 0) {
		for (var i = 0; i < roots.length; i++) {
			var stationNode = roots[i];
			if (stationId == stationNode.id) {
				findStation = true;
				$("#ambTree").tree("append", {
					parent : stationNode.target,
					data : [{
						id : ambId,
						text : ambRealSign,
						iconCls : "icon-ambulance"
					}]
				});

				break;
				//找到后跳出循环
			}
		}
	}

	if (!findStation) {
		var newSD = $("#ambTree").tree("append", {
			data : [{
				id : stationId,
				text : stationName,
				iconCls : "icon-station"
			}]
		});
		roots = $("#ambTree").tree("getRoots");
		$("#ambTree").tree("append", {
			parent : roots[roots.length - 1].target,
			data : [{
				id : ambId,
				text : ambRealSign,
				iconCls : "icon-ambulance"
			}]
		});
	}
}

function deleteAmbFromTree(ambId, stationId) {
	var roots = $("#ambTree").tree("getRoots");
	if (roots && roots.length > 0) {
		for (var i = 0; i < roots.length; i++) {
			var stationNode = roots[i];
			if (stationId == stationNode.id) {//找到了分站节点
				var ambNodes = $("#ambTree").tree("getChildren", stationNode.target);
				var ambNodesLength = ambNodes.length;
				for (var j = 0; j < ambNodesLength; j++) {
					var ambNode = ambNodes[j];
					if (ambNode.id == ambId) {//找到车辆
						$("#ambTree").tree("remove", ambNode.target);
						if (ambNodesLength == 1) {//只有一个节点, 删除分站节点
							$("#ambTree").tree("remove", stationNode.target);
						}
						break;
					}
				}
			}
		}
	}
}

function testMethod() {
	//addAmbToTree("00001","车辆1", "001", "分站一");
	//addAmbToTree("00002","车辆2", "001", "分站一");
	//addAmbToTree("00003","车辆3", "002", "分站二");
	//addAmbToTree("00004","车辆4", "002", "分站二");
	//addAmbToTree("00005","车辆5", "003", "分站三");
	//addAmbToTree("00006","车辆6", "004", "分站四");
	//addAmbToTree("00007","车辆7", "004", "分站四");
	setSpotCircleRadius(2000);
}

function testMethod2() {
	/*
	 var pointArray = [];//轨迹点数组，用于调整地图视野
	 pointArray.push(new BMap.Point(116.1213, 39.224));
	 pointArray.push(new BMap.Point(116.1220, 39.224));
	 pointArray.push(new BMap.Point(116.1230, 39.224));
	 pointArray.push(new BMap.Point(116.1240, 39.224));
	 pointArray.push(new BMap.Point(116.1250, 39.224));
	 lushu = new BMapLib.LuShu(map,pointArray,
	 {
	 icon:new BMap.Icon("images/gpstrack_r.png", new BMap.Size(50,50)),
	 speed: 2000,
	 enableRotation:true,
	 autoView:true, //是否开启自动视野调整，如果开启那么路书在运动过程中会根据视野自动调整
	 landmarkPois:[]
	 });
	 lushu.start();
	 */
	changeSpotCircleRadius(3000);
}


//add 2020-10-10 plq toolBar 防病车与医院保持统一 start
function btnAmbDown() {
    //Gid("btnAmbulance").src = "../../images/control2/amb_2.png";
    Gid("btnAmbulance").src = iconUrlHead + "images/control2/amb_2.png";

    $('#ambWindow').window("open");
    $("#ambGrid").datagrid("loadData", fbAmbArray.slice(0, 10));
    //分页
    var pager = $("#ambGrid").datagrid("getPager");
    pager.pagination({
        total: fbAmbArray.length,
        onSelectPage: function (pageNo, pageSize) {
            var start = (pageNo - 1) * pageSize;
            var end = start + pageSize;
            $("#ambGrid").datagrid("loadData", fbAmbArray.slice(start, end));
            pager.pagination('refresh', {
                total: fbAmbArray.length,
                pageNumber: pageNo
            });
        }
    });
}

function btnAmbUp() {
    //Gid("btnAmbulance").src = "../../images/control2/amb_1.png";
    Gid("btnAmbulance").src = iconUrlHead + "images/control2/amb_1.png";
}
//end


//隐藏所有车辆的checkbox点击事件 add2020-11-09 plq
function hideAmbClick() {

    if ($("#hideamb")[0].src.indexOf('Images/control/check.png') != -1) {
        hideAllAmb(true);
        $("#hideamb").attr('src', iconUrlHead + 'Images/control/checked.png');
    }
    else {
        hideAllAmb(false);
        $("#hideamb").attr('src', iconUrlHead + 'Images/control/check.png');
    }

    /*
	if ($("#hideamb").is(':checked')) {
		alert("checked");
	} else {
		alert("unchecked");
	}
	*/
}

